package com.siyu.prewarn.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.siyu.basedata.plan.domain.Plan;
import com.siyu.basedata.plan.service.IPlanService;
import com.siyu.common.core.controller.BaseController;
import com.siyu.common.core.domain.AjaxResult;
import com.siyu.common.utils.uuid.IdUtils;
import com.siyu.prewarn.dto.SendDto;
import com.siyu.prewarn.dto.TimeDto;
import com.siyu.prewarn.mesuser.domain.Mesuser;
import com.siyu.prewarn.mesuser.service.IMesuserService;
import com.siyu.prewarn.sendway.service.ISendWayService;
import com.siyu.prewarn.service.IPrewarnService;
import com.siyu.prewarn.skreservoirdutyperson.domain.SkReservoirDutyPerson;
import com.siyu.prewarn.skreservoirdutyperson.service.ISkReservoirDutyPersonService;
import com.siyu.prewarn.warncondition.domain.WarnCondition;
import com.siyu.prewarn.warncondition.service.IWarnConditionService;
import com.siyu.prewarn.warnuser.domain.WarnUser;
import com.siyu.prewarn.warnuser.service.IWarnUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Random;

@RestController
@RequestMapping("/prewarn")
public class PrewarnController extends BaseController {

    @Autowired
    private IPrewarnService prewarnService;
    @Autowired
    private IMesuserService mesuserService;
    @Autowired
    private IWarnUserService warnUserService;
    @Autowired
    private ISendWayService sendWayService;
    @Autowired
    private IPlanService planService;
    @Autowired
    private ISkReservoirDutyPersonService skReservoirDutyPersonService;


    /**
     * 获取雨量数据
     *
     * @param tms
     * @param rscd
     * @return
     */
    @GetMapping("/monitor/raindata")
    private AjaxResult getRaindata(@RequestParam("tms") Integer tms,
                                   @RequestParam("rscd") String rscd) throws Exception {
        HashMap<Object, Object> xajResults = prewarnService.getRaindata(tms, rscd);
        return AjaxResult.success(xajResults);
    }

    /**
     * 查询监测报警信息
     */
    @GetMapping("/plan/getwarndata")
    public AjaxResult getWarnData(@RequestParam("rscd") String rscd) {
        try {
            HashMap<Object, Object> map = prewarnService.getWarnData(rscd);
            return AjaxResult.success(map);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 查询所有预警对象列表
     *
     * @return
     */
    @GetMapping("/mesuser/listall")
    public AjaxResult getMesUserList(String rscd) {
        return AjaxResult.success(skReservoirDutyPersonService.lambdaQuery().eq(SkReservoirDutyPerson::getRscd, rscd).list());
    }

    /**
     * 新增预警对象
     */
    @PostMapping("/mesuser/insert")
    public AjaxResult insertMesuser(@RequestBody SkReservoirDutyPerson skReservoirDutyPerson) {
        try {
            // 判断用户电话是否在数据库存在
            if (skReservoirDutyPersonService.list(new QueryWrapper<SkReservoirDutyPerson>().eq("mobile", skReservoirDutyPerson.getMobile())).size() != 0)
                return AjaxResult.error("该电话号码已存在！");
            skReservoirDutyPerson.setId(Long.valueOf(IdUtils.getId()));
            skReservoirDutyPerson.setDutyPerson("管理员");
            skReservoirDutyPersonService.insertSkReservoirDutyPerson(skReservoirDutyPerson);
            return AjaxResult.success("新增成功！");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 新增预警对象
     */
    @PutMapping("/mesuser/update")
    public AjaxResult updateMesuser(@RequestBody SkReservoirDutyPerson skReservoirDutyPerson) {
        try {
            // 判断用户电话是否在数据库存在
            if (skReservoirDutyPersonService.list(new QueryWrapper<SkReservoirDutyPerson>().eq("mobile", skReservoirDutyPerson.getMobile())).size() > 1)
                return AjaxResult.error("该电话号码已存在！");
            skReservoirDutyPersonService.updateSkReservoirDutyPerson(skReservoirDutyPerson);
            return AjaxResult.success("修改成功！");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 删除预警对象
     */
    @DeleteMapping("/mesuser/del")
    @Transactional
    public AjaxResult del(String id) {
        try {
            // 删除预警对象
            skReservoirDutyPersonService.deleteSkReservoirDutyPersonById(Long.valueOf(id));
            // 删除关联信息
            warnUserService.remove(new QueryWrapper<WarnUser>().eq("user_id", id));
            return AjaxResult.success("删除成功！");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 信息发送方式查询
     *
     * @return
     */
    @GetMapping("/warnway/list")
    public AjaxResult warnWayList() {
        return AjaxResult.success(sendWayService.list());
    }

    /**
     * 查询所有方案列表
     *
     * @param rscd
     * @return
     */
    @GetMapping("/plan/getallplan")
    public AjaxResult getAllPlan(String rscd) {
        QueryWrapper<Plan> planQueryWrapper = new QueryWrapper<>();
        planQueryWrapper.select("id", "plan_name", "plan_type")
                .eq("rscd", rscd)
                .orderByDesc("end_time")
                .ne("plan_type","假拟")
                .eq("pre_type", "预报");
        return AjaxResult.success(planService.list(planQueryWrapper));
    }

    /**
     * 查询预警方案详情
     *
     * @param id
     * @return
     */
    @GetMapping("/plan/getwarnplandetail")
    public AjaxResult getWarnPlanDetail(String id) {
        return AjaxResult.success(prewarnService.getData(id));
    }

    /**
     * 预警结果列表
     *
     * @param timeDto
     * @return
     */
    @GetMapping("/warnmes/list")
    public AjaxResult warnmesList(TimeDto timeDto) {
        try {
            List<WarnCondition> list = prewarnService.getWarnMes(timeDto);
            return AjaxResult.success(list);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 预警消息发送
     *
     * @param sendDto
     * @return
     */
    @PostMapping("/warnmes/save")
    public AjaxResult sendWarnMes(@RequestBody SendDto sendDto) {
        try {
            prewarnService.saveWarnMes(sendDto);
            return AjaxResult.success("发送成功！");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
